package com.jiuzhou.guanwang.jzcp.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.google.gson.Gson;
import com.jiuzhou.guanwang.jzcp.R;
import com.jiuzhou.guanwang.jzcp.adapter.TeamRankAdapter;
import com.jiuzhou.guanwang.jzcp.base.BaseFragment;
import com.jiuzhou.guanwang.jzcp.bean.RankResult;
import com.jiuzhou.guanwang.jzcp.bean.StandingsInfo;
import com.jiuzhou.guanwang.jzcp.netutil.NetRequestUtil;
import com.jiuzhou.guanwang.jzcp.utils.CommonUtils;
import com.vise.log.ViseLog;

import java.util.HashMap;
import java.util.Map;

public class TeamRankDetailFragment extends BaseFragment {
    private TeamRankAdapter mAdapter;
    private ListView mList;
    private String mName;

    protected void initView(View view) {
        this.mList = (ListView) view.findViewById(R.id.lv_list);
    }

    protected int bindLayout() {
        return R.layout.fragment_team_rank_detail;
    }

    protected void initData() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            this.mName = arguments.getString("name");
        } else {
            this.mName = "yc";
        }
        String code = "";
        switch (mName) {
            case "dj":
                code = "9";
                break;
            case "fj":
                code = "16";
                break;
            case "xj":
                code = "7";
                break;
            case "yc":
                code = "8";
                break;
            case "yj":
                code = "13";
                break;
        }
        Map<String, String> map = new HashMap<>();
        map.put("leagueId", code);
        NetRequestUtil.getInstance().get("http://zx.caipiao.163.com/libraryapi/league_matchRank.html", map, 100, new NetRequestUtil.NetResponseListener<String>() {
            @Override
            public void onSuccess(String response, int requestCode) {
                ViseLog.d(response);
                RankResult result = (RankResult) new Gson().fromJson(response, RankResult.class);
                if (result != null && !CommonUtils.isEmpty(result.standings) && !CommonUtils.isEmpty(((StandingsInfo) result.standings.get(0)).teamInfo)) {
                    TeamRankDetailFragment.this.mAdapter = new TeamRankAdapter(TeamRankDetailFragment.this.getActivity());
                    TeamRankDetailFragment.this.mList.setAdapter(TeamRankDetailFragment.this.mAdapter);
                    TeamRankDetailFragment.this.mAdapter.setData(((StandingsInfo) result.standings.get(0)).teamInfo);
                }

            }

            @Override
            public void onFailed(String response, int requestCode) {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    public static TeamRankDetailFragment newInstance(String name) {
        Bundle bundle = new Bundle();
        bundle.putString("name", name);
        TeamRankDetailFragment teamRankDetailFragment = new TeamRankDetailFragment();
        teamRankDetailFragment.setArguments(bundle);
        return teamRankDetailFragment;
    }
}